#include <iostream>

#include "oj_control.hpp"
#include "../comm/httplib.h"

using namespace httplib;
using namespace dsh_control;

int main()
{
    // 用户请求的服务路由功能
    Server svr;
    Control ctrl;

    // 获取所有的题目列表
    svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp){
        std::string html;
        ctrl.AllQuestions(&html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 用户要根据题目编号获取题目内容
    // /question/100 -> 正则匹配，\d表示匹配数字，+表示匹配多个
    // R"()", raw string, 保持原始字符串
    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1]; // Request中的Match matches下标为1的位置存放正则匹配的结构
        std::string html;
        ctrl.Question(number, &html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 用户提交代码，使用判题功能（判断测试用例，compile_and_run）
    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1];
        std::string result_json;;
        ctrl.Judge(number, req.body, &result_json);
        resp.set_content(result_json, "application/json;charset=utf-8");
    });

    svr.set_base_dir("./wwwroot/");

    svr.listen("0.0.0.0", 8080);

    return 0;
}